//
//  CTCUpdatePwdVC.m
//  ConsultantVersion
//
//  Created by 小龙 on 2017/4/14.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "CTCUpdatePwdVC.h"
#import "UMessage.h"

@interface CTCUpdatePwdVC ()
@property (weak, nonatomic) IBOutlet UITextField *oldPwdField;
@property (weak, nonatomic) IBOutlet UITextField *firstNewPwdField;
@property (weak, nonatomic) IBOutlet UITextField *secondNewPwdField;

@end

@implementation CTCUpdatePwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"修改密码";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon-denglu-fanhui副本"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnClik)];
    self.oldPwdField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.oldPwdField.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.firstNewPwdField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.firstNewPwdField.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.secondNewPwdField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.secondNewPwdField.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setTitleTextAttributes:@{NSFontAttributeName:SYSTEMFONT(17),NSForegroundColorAttributeName:[UIColor clearColor]}];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //去除导航条透明后导航条下的黑线
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //    self.automaticallyAdjustsScrollViewInsets = YES;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:HEXCOLOR(0x2e8ce5)]
                                                  forBarMetrics:UIBarMetricsDefault];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)submitBtnClick
{
    [self.view endEditing:YES];
    
    BOOL isRegularPwd = self.oldPwdField.text.length && [self.firstNewPwdField.text isEqualToString:self.secondNewPwdField.text];
    
    if (!isRegularPwd) {
        [MBProgressHUD showErrorMessage:@"请输入正确的密码"];
        return;
    }
    
    
    [self updatePwdRequest];
    
    
}


- (void)updatePwdRequest
{
    [MBProgressHUD showAnimationHUD];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"newPwd"] = [self.firstNewPwdField.text hyb_toMD5];
    dic[@"oldPwd"] = [self.oldPwdField.text hyb_toMD5];
    [RequestTool POST:kUpdatePwdUrl parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"responseObject:%@", responseObject);
        if ([responseObject[@"code"] integerValue] == 1) {
            [MBProgressHUD showSuccessMessage:responseObject[@"des"]];
            [self loginRequest];
        }
        else {
            [MBProgressHUD showErrorMessage:responseObject[@"des"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"error:%@", error);
        
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力，请检查网络"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }
    }];
}


- (void)loginRequest
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"pwd"] = [self.firstNewPwdField.text hyb_toMD5];
    dic[@"accountId"] = [CTCUserModel shareUser].detailModel.uuid;
    dic[@"deviceType"] =@0;
    [RequestTool POST:kLoginUrl parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"responseObject:%@", responseObject);
        if ([responseObject[@"code"] integerValue] == 1) {
            [MBProgressHUD showSuccessMessage:responseObject[@"des"]];
            NSDictionary *data = responseObject[@"data"];
            if (data) {
                NSString *cookieID = data[@"cookieId"];
                [NetManager refreshCookieId:cookieID];
                [CTCUserModel shareUser].cookieID = cookieID;
                [CTCUserModel shareUser].login = YES;
                
                [CTCUserModel shareUser].detailModel = [CTCUserDetailModel yy_modelWithJSON:data];
                
                [self bindAliasWithAccout];
            }

            
            if (self.updatePwdSuccessReturnBlock) {
                self.updatePwdSuccessReturnBlock();
            }
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else {
            [MBProgressHUD showErrorMessage:responseObject[@"des"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DeLog(@"error:%@", error);
        
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力，请检查网络"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }
    }];
}


// 使用帐号绑定alias
- (void)bindAliasWithAccout {
    
    if ([CTCUserModel shareUser].detailModel.uuid) {
        
        dispatch_queue_t queue = dispatch_queue_create(0, 0);
        dispatch_async(queue, ^{
            
            [UMessage setAlias:[CTCUserModel shareUser].detailModel.uuid type:@"alias" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
                
                if (error) {
                    DeLog(@"set alias err %@", error);
                }else {
                    DeLog(@"set alias successfully--%@" ,responseObject);
                }
                
            }];
            
        });
    }
    
}


- (void)backBtnClik
{
    [self.view endEditing:YES];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
